﻿//-------------------------[Dialog2]-----------------------------------------------------
/*
    功能：显示一个对话框
    
    前置支持：Tools.js
*/
function Dialog2(dlgID, title, modal, width, zIdx, top, left, styleCss, openCallFunc, closeCallFunc)
{
    var T = this;
    this.zIdxStyle = (zIdx != null) ? (";z-index:" + parseInt(zIdx).toString() + ";") : "";
    this.AddStyle = (styleCss != null && styleCss != "") ? styleCss : ""; //最外层容器增加的style
    T.AddStyle += T.zIdxStyle;
    
    
    // 对话框内容对象
    this.Content = document.getElementById(dlgID);
    
    // 对话框容器
    this.DlgBox = document.createElement("div");
    T.DlgBox.style.cssText =   "width:"  + width +   ";position:absolute;border:1px solid #cccccc; padding:2px; display:none;background-color:#FFFFFF;" + T.AddStyle;
    
    // 对话框虚线边框
    this.DlgBorder = document.createElement("div");
    T.DlgBorder.style.cssText = "border:1px  outset #EEEEEE; ";

    // 对话框标题
    this.TitleTxt = title;
    this.Title = document.createElement("div");
    T.Title.style.cssText = "cursor:move;font-family:宋体;font-size:12px; font-weight:bold; padding-left:0.5em; background-color:#018A6E; color:#FFFFFF;line-height:24px;height:24px;";
    
    this.TitleTxtObj = document.createElement("span");
    T.TitleTxtObj.style.cssText = "float:left;";
    T.TitleTxtObj.innerHTML = T.TitleTxt; 
    
    T.Title.appendChild(T.TitleTxtObj);
    
    this.BtClose = document.createElement("span");
    T.BtClose.innerHTML = "×";
    T.BtClose.style.cssText = "font-size:16px;color:red; float:right; font-weight:bold;line-height:24px; padding-right:6px;cursor:pointer;";
    T.BtClose.title = "关闭";
    T.Title.appendChild(T.BtClose);
    

    // 屏蔽层
    this.MaskAlpha = 50;        // 透明度(1-100)
    this.MaskColor = "#99aacc"; // 颜色
    this.Mask = document.createElement("div");
    this.Mask.style.cssText = "position:absolute;	 left:0px;  top:0px;  filter:alpha(opacity=" + T.MaskAlpha + ");  -moz-opacity: " + (T.MaskAlpha / 100) + ";  opacity: " + (T.MaskAlpha / 100) + ";	background-color:" + T.MaskColor + ";  display:none;" + T.zIdxStyle;
    
	
	// 初始化
    this.Init = function()
    {
        T.Content.parentNode.appendChild(T.Mask);
        T.Content.parentNode.appendChild(T.DlgBox);
        T.DlgBox.appendChild(T.DlgBorder);
        T.DlgBorder.appendChild(T.Title);
        T.DlgBorder.appendChild(T.Content);

        if (!modal)
        {
            //非模态窗口，点击屏蔽区域可以关闭对话框.
            addEventHandler(T.Mask, "click", T.Hide.bind(T));
        }
        addEventHandler(window, "resize", T.resizeMask.bind(T));
        addEventHandler(T.BtClose, "click", T.Hide.bind(T));
        
        Drag.init(T.Title,T.DlgBox);
    };

    // 遮罩、对话框位置、大小处理，对话框移动区域设定
    this.SetXYWH = function(ofx, ofy)
    {
    	var sz = GetPageSize();
    	var wh = getLTWH(T.DlgBox);
    	T.Mask.style.width = sz.PageW;
    	T.Mask.style.height = sz.PageH;
    	if (left != null)
    	{
    		T.DlgBox.style.left = left;
    	}
    	else
    	{
    		T.DlgBox.style.left = parseInt((sz.WinW - wh.width) / 2) + "px";
    	}
    	if (null != ofx && !isNaN(ofx))
    	{
    		T.DlgBox.style.left = (parseInt(T.DlgBox.style.left) + ofx) + "px";
    	}

    	if (top != null)
    	{
    		T.DlgBox.style.top = top;
    	}
    	else
    	{
    		T.DlgBox.style.top = parseInt((sz.WinH - wh.height) / 3) + "px";
    	}
    	if (null != ofy && !isNaN(ofy))
    	{
    		T.DlgBox.style.top = (parseInt(T.DlgBox.style.top) + ofy) + "px";
    	}

    	T.Title.minX = 0;
    	T.Title.minY = 0;
    	T.Title.maxX = sz.WinW - wh.width;
    	T.Title.maxY = sz.WinH - wh.height;
    }
   
	// 窗口大小改变事件处理
    this.resizeMask = function()
    {
        if (T.Mask.style.display == "none")
        {
            return;
        }
        T.SetXYWH();
    };
	//显示
    this.Show = function (titleTxt, ofX, ofY) {
        if (titleTxt) {
            T.TitleTxtObj.innerHTML = titleTxt;
        }
        else {
            T.TitleTxtObj.innerHTML = T.TitleTxt;
        }
        T.Mask.style.display = "";
        T.DlgBox.style.display = "";
        T.SetXYWH(ofX, ofY);

        if (openCallFunc) {
            openCallFunc();
        }
    };
    //隐藏
	this.Hide = function()
	{
	    T.Mask.style.display   = "none";
	    T.DlgBox.style.display = "none";
	    if (closeCallFunc)
	    {
	        closeCallFunc();
	    }
	};
	//调用 初始化
	T.Init();
}

